package com.example.shop_2304.minefg

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.PopupWindow
import android.widget.TextView
import androidx.appcompat.app.ActionBar.LayoutParams
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseFragment
import com.example.base.param.PRefund
import com.example.shop_2304.R
import com.example.shop_2304.adapter.OrderAdapter
import com.example.shop_2304.databinding.FragmentPay2Binding
import com.example.shop_2304.databinding.FragmentPayBinding
import com.example.shop_2304.room.AppDbUtils
import com.example.shop_2304.room.OrderRoom

/**
 * 待发货界面
 */
class Pay2Fragment  : BaseFragment<FragmentPay2Binding, PayViewModel>() {
    lateinit var orderAdapter: OrderAdapter
    override fun initView() {
        orderAdapter = OrderAdapter()
        binding.rv.adapter = orderAdapter
        binding.rv.layoutManager = LinearLayoutManager(context)

        viewModel.order("1",10,1)

        orderAdapter.addChildClickViewIds(R.id.order_tv2,R.id.order_tv)
        orderAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.order_tv2->{
                    //弹出选择框
                    val popupWindow = PopupWindow(context)
                    popupWindow.width = LayoutParams.MATCH_PARENT
                    popupWindow.height = LayoutParams.WRAP_CONTENT
                    val view1 = LayoutInflater.from(context).inflate(R.layout.item_refund, null)
                    view1.findViewById<TextView>(R.id.tv_amount).text = "退款金额:${orderAdapter.data[position].payPrice}"
                    view1.findViewById<TextView>(R.id.tv1).setOnClickListener {
                        view1.findViewById<TextView>(R.id.tv1).setTextColor(Color.RED)
                        view1.findViewById<TextView>(R.id.tv2).setTextColor(Color.BLACK)
                    }
                    view1.findViewById<TextView>(R.id.tv2).setOnClickListener {
                        view1.findViewById<TextView>(R.id.tv2).setTextColor(Color.RED)
                        view1.findViewById<TextView>(R.id.tv1).setTextColor(Color.BLACK)
                    }
                    view1.findViewById<Button>(R.id.btn).setOnClickListener {
                        val pRefund = PRefund()
                        pRefund.id = orderAdapter.data[position].id
                        pRefund.uni = orderAdapter.data[position].orderId
                        viewModel.refund(pRefund)

                        orderAdapter.data.removeAt(position)
                        orderAdapter.notifyItemRemoved(position)

                        popupWindow.dismiss()
                    }
                    popupWindow.contentView = view1
                    popupWindow.isOutsideTouchable = true
                    popupWindow.isFocusable = true
                    popupWindow.showAtLocation(view,Gravity.BOTTOM,0,0)
                }
                R.id.order_tv->{
                    //先吐司，保存数据库  页面删除
                    ToastUtils.showLong("催商家发货成功，商家已发货")
                    val order = orderAdapter.data[position]
                    val orderRoom = OrderRoom(order.id,order.orderId,order.createTime,"待收货",order.payPrice,
                        order.totalNum,order.orderInfoList[0].cartNum,order.orderInfoList[0].image,
                        order.orderInfoList[0].price,order.orderInfoList[0].productId,order.orderInfoList[0].storeName)
                    AppDbUtils.getDb().getOrderDao().insert(orderRoom)

                    orderAdapter.data.removeAt(position)
                    orderAdapter.notifyItemRemoved(position)
                }
            }
        }
    }

    override fun initData() {
        viewModel.success.observe(this){
            if(it.code==200){
                orderAdapter.data.clear()
                orderAdapter.data.addAll(it.data.list)
                orderAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.refundSuccess.observe(this){
            ToastUtils.showLong(it.message)
            //最好是刷新一下界面  或者我们直接在页面上移除这条数据
        }
    }

    override fun getLayout(): Int = R.layout.fragment_pay2

    override fun getViewModel(): Class<PayViewModel>  = PayViewModel::class.java

}